//
//  RewardWallViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/4/14.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "RewardWallViewController.h"
#import "WallCollectionHeadView.h"
#import "WishCollectionViewCell.h"
#import "MyWishViewController.h"
#import "VowViewController.h"
#import "RewardView.h"
#import "AlertWishView.h"
#import "ShopDetailViewController.h"
#import "LMWishModel.h"
#import "LMWallTableViewCell.h"

@interface RewardWallViewController ()<RewardViewDelegate,alertShareDelegate,UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong)UITableView *tableView;
@property (nonatomic ,strong)AlertWishView*alert;
@property (nonatomic,strong)UIScrollView*mainScroView;
@property (nonatomic,strong)RewardView *rewardView;
@property (nonatomic,strong)NSMutableArray *array;
@property (nonatomic,assign) int pageNo;
@property (nonatomic,strong)MJRefreshBackNormalFooter*mj_footer;


@property (nonatomic ,strong)LMWishModel *model;

@end

@implementation RewardWallViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    UIBarButtonItem*item=[[UIBarButtonItem alloc]initWithTitle:@"祈愿" style:UIBarButtonItemStyleDone target:self action:@selector(detailClick)];
    [item setTitleTextAttributes:@{NSFontAttributeName:FontAndStyle14,NSForegroundColorAttributeName:[UIColor whiteColor]} forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem=item;
    [self.array removeAllObjects];
    self.pageNo=1;
   [self loadRequest];
}
-(void)detailClick{
    VowViewController *vc=[[VowViewController alloc]init];
    vc.title=@"祈愿";
    [self.navigationController pushViewController:vc animated:YES];
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, MainW, MainH-64) style:UITableViewStyleGrouped];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.separatorColor=[UIColor clearColor];
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIImageView *backImageView=[[UIImageView alloc]initWithFrame:self.view.bounds];
    [backImageView setImage:[UIImage imageNamed:@"image_wish"]];
    self.tableView.backgroundView=backImageView;
    _array=[NSMutableArray array];
    UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, MainW, MainH-63)];
    image.image=[UIImage imageNamed:@"image_wish"];
    [self.view addSubview:image];
     [self.view addSubview:self.mainScroView];
    [self showTempView:@"暂无心愿" Image:@"img_fail_monk" CGFrame:CGRectMake(0, 0, MainW,MainH-164)];
    self.tempView.hidden=YES;
    // Do any additional setup after loading the view.
}
-(UIScrollView*)mainScroView{
    if (!_mainScroView) {
        //__weak typeof(self) weakSelf = self;
        _mainScroView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, MainW, MainH-64)];
        _mainScroView.backgroundColor = [UIColor clearColor];
        _mainScroView.contentSize = CGSizeMake(0, MainH-64);
        //_mainScroView.delegate = self;
        _mainScroView.showsHorizontalScrollIndicator = NO;
        _mainScroView.showsVerticalScrollIndicator = NO;
        _mainScroView.mj_footer=[MJRefreshBackStateFooter footerWithRefreshingBlock:^{
            NSLog(@"你麻痹" );
            self.pageNo++;
            [self loadRequest];
            [_mainScroView.mj_footer endRefreshing];
        }];
        
    }
    return _mainScroView;
}
-(void)footerRefresh{
    
}
-(void)creatTatoleView:(NSArray*)listArray{
    if (listArray.count==0) {
        self.tempView.hidden=NO;
        return;
    }else{
        self.tempView.hidden=YES;
    }
    for (UIView *view in self.mainScroView.subviews) {
        if (view!=_mainScroView.mj_footer) {
            [view removeFromSuperview];
        }
        
    }
    CGFloat maxY = 00;
    CGFloat height = 0;
    for (int i = 0; i < listArray.count; i ++)
    {
        
        self.rewardView = [[RewardView alloc] initWithArray:listArray[i] andFrame:CGRectMake(0, maxY, MainW, 60)];
        maxY = CGRectGetMaxY(self.rewardView.frame)+10;
        height += self.rewardView.bsg_height;
        self.rewardView.tag = 800+i;
        self.rewardView.delegate = self;
        //self.rewardView.backgroundColor=[UIColor cyanColor];
        [self.mainScroView addSubview:self.rewardView];
    }
    //self.offetHeight=height;
    self.mainScroView.contentSize = CGSizeMake(0, height+listArray.count*10);
}
-(void)loadRequest{
    NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];
    dic[@"pageSize"]=@(10);
    dic[@"pageNumber"]=@(self.pageNo);
    [self doHttpRequestWithApi:userpraylist method:POST dic:dic successBlock:^(NSDictionary *dic) {
        
        [self.array addObjectsFromArray:dic[@"data"][@"list"]];
        [self creatTatoleView:self.array];
        //[self.tableView reloadData];
    }];
}
-(void)selectBtnTitleandBtn:(NSDictionary *)btn{
    NSLog(@"%@----",btn);
    self.alert=[[AlertWishView alloc]initWithFrame:CGRectMake(0, 0, MainW, MainH)];
    self.alert.shareDic=btn;
    self.alert.delegate=self;
     //NSLog(@"****%@----",self.alert.shareDic);
    [self.view addSubview:self.alert];
}
-(void)shopClickDetail{
    ShopDetailViewController*vc=[[ShopDetailViewController alloc]init];
    vc.spuId=self.alert.shareDic[@"skuId"];
    [self.navigationController pushViewController:vc animated:YES];
    self.shadowView.hidden=YES;
    self.alert.hidden=YES;
}
-(void)cancleClickShare{
    [self.alert endEditing:YES];
    [UIView animateWithDuration:0.2 animations:^{
        self.alert.alpha=0;
        self.shadowView.hidden=YES;
    } completion:^(BOOL finished) {
        self.alert.hidden=YES;
        
    }];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.array.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LMWishModel *model=self.array[indexPath.section];
    static NSString *cellidentifier=@"cell";
    LMWallTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellidentifier];;
    if (!cell) {
        cell=[[LMWallTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];
    }
    cell.backgroundColor=[UIColor clearColor];
    [cell setModel:model];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
@end
